package com.hankol.newretail.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.baselibrary.Constants;
import com.baselibrary.manager.SensorsDataManager;
import com.baselibrary.router.ActivityRouter;
import com.baselibrary.utils.ARouterUtils;
import com.baselibrary.utils.AmountUtil;
import com.baselibrary.utils.GlideUtils;
import com.baselibrary.utils.SpecUtils;
import com.baselibrary.utils.ToastUtils;
import com.baselibrary.view.RoundCornerImageView;
import com.hankol.newretail.R;
import com.hankol.newretail.adapter.NeedPayListAdapter;
import com.hankol.newretail.bean.goods_details.OrderListSkuDtoList;
import com.hankol.newretail.bean.goods_details.WaitPayOrderList;

import java.util.ArrayList;
import java.util.HashMap;

import cn.iwgang.countdownview.CountdownView;

/**
 * @author:hanke 2022/9/6
 * desc:
 */
public class RetailDialogUtils {
    /**
     * 待支付订单
     *
     * @param act
     */
    public static AlertDialog showNeedPayDialog(Activity act, WaitPayOrderList item) {
        final AlertDialog dialog = new AlertDialog.Builder(act, AlertDialog.THEME_HOLO_DARK).create();
        dialog.setCancelable(true);
        dialog.show();
        Window window = dialog.getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.getDecorView().setBackgroundColor(act.getResources().getColor(R.color.transparent));
        window.setAttributes(lp);
        window.setContentView(R.layout.dialog_need_pay);
        window.setGravity(Gravity.CENTER);
        RelativeLayout rlSingle = window.findViewById(R.id.rl_single_goods_order);
        RelativeLayout rlMultiple = window.findViewById(R.id.rl_multi_goods_order);
        if (item != null && item.getOrderListSkuDtoList().size() > 1) {
            rlMultiple.setVisibility(View.VISIBLE);
            rlSingle.setVisibility(View.GONE);
            RecyclerView rvCover = window.findViewById(R.id.rv_order_cover);
            TextView totalPrice = window.findViewById(R.id.tv_order_total_price);
            totalPrice.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    HashMap hashMap = new HashMap();
                    hashMap.put(Constants.KEY_ORDER_ID, item.getPoId());
                    ARouterUtils.jump(ActivityRouter.NEW_RETAIL.ORDER_DETAIL, hashMap);
                }
            });
            NeedPayListAdapter.OrderCoverListAdapter orderCoverListAdapter = new NeedPayListAdapter.OrderCoverListAdapter();
            rvCover.setAdapter(orderCoverListAdapter);
            orderCoverListAdapter.setNewData(item.getOrderListSkuDtoList());
            orderCoverListAdapter.setOnItemClickListener((adapter, view, position) -> {
                HashMap hashMap = new HashMap();
                hashMap.put(Constants.KEY_ORDER_ID, item.getPoId());
                ARouterUtils.jump(ActivityRouter.NEW_RETAIL.ORDER_DETAIL, hashMap);
            });
            try {
                String priceShow = "共" + item.getNum() + "件，合计：¥" + AmountUtil.changeF2YKeepTwo(String.valueOf(item.getPrice()));
                totalPrice.setText(priceShow);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (item != null && item.getOrderListSkuDtoList().size() == 1) {
            rlMultiple.setVisibility(View.GONE);
            rlSingle.setVisibility(View.VISIBLE);
            rlSingle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    HashMap hashMap = new HashMap();
                    hashMap.put(Constants.KEY_ORDER_ID, item.getPoId());
                    ARouterUtils.jump(ActivityRouter.NEW_RETAIL.ORDER_DETAIL, hashMap);
                }
            });
            RoundCornerImageView cover = window.findViewById(R.id.rci_order_cover);
            TextView title = window.findViewById(R.id.tv_order_title);
            TextView price = window.findViewById(R.id.tv_order_price);
            OrderListSkuDtoList product = item.getOrderListSkuDtoList().get(0);
            GlideUtils.loadImg_228(act, product.getSkuCover(), cover);
            title.setText(product.getSpuName());
            try {
                price.setText("合计：¥" + AmountUtil.changeF2YKeepTwo(String.valueOf(product.getPrice())));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        CountdownView tvLeftTime = window.findViewById(R.id.cdv_pay_left_time);
        tvLeftTime.start(item.getOvertime() - System.currentTimeMillis());
        tvLeftTime.setOnCountdownEndListener(new CountdownView.OnCountdownEndListener() {
            @Override
            public void onEnd(CountdownView cv) {
                tvLeftTime.stop();
                dialog.cancel();
                ToastUtils.showShort(act, "订单已自动取消");
            }
        });
        TextView tvPayCancel = window.findViewById(R.id.tv_pay_cancel);
        TextView tvPayNow = window.findViewById(R.id.tv_pay_now);
        ArrayList idList = new ArrayList();
        ArrayList nameList = new ArrayList();
        ArrayList specList = new ArrayList();
        for (OrderListSkuDtoList data : item.getOrderListSkuDtoList()) {
            idList.add(data.getSpuId());
            nameList.add(data.getSpuName());
            specList.add(SpecUtils.INSTANCE.getNormsStr(data.getSpecList()));
        }
        tvPayNow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SensorsDataManager.Companion.getInstance().productDetailPayment(item.getPoId() + "", idList, item.getShopName(), nameList, specList);
                HashMap hashMap = new HashMap();
                hashMap.put(Constants.KEY_ORDER_ID, item.getPoId());
                ARouterUtils.jump(ActivityRouter.NEW_RETAIL.ORDER_DETAIL, hashMap);
            }
        });
        tvPayCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                tvLeftTime.stop();
            }
        });
        return dialog;
    }

    public static void showCancelEvaluationDialog(Activity act) {
        final AlertDialog dialog = new AlertDialog.Builder(act, AlertDialog.THEME_HOLO_DARK).create();
        dialog.setCancelable(true);
        dialog.show();
        Window window = dialog.getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.getDecorView().setBackgroundColor(act.getResources().getColor(R.color.transparent));
        window.setAttributes(lp);
        window.setContentView(R.layout.dialog_evaluation_cancel);
        window.setGravity(Gravity.CENTER);
        window.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });
        window.findViewById(R.id.tv_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                act.finish();
            }
        });
    }
}
